package com.openlogic.system.rpc;

import java.util.List;
import java.util.Set;

import org.noear.solon.annotation.Inject;
import org.noear.solon.annotation.Mapping;
import org.noear.solon.annotation.Remoting;
import org.noear.solon.core.handle.Result;

import com.openlogic.common.core.constants.ServiceConstant;
import com.openlogic.common.core.context.SecurityContextHolder;
import com.openlogic.common.core.utils.HeadersUtil;
import com.openlogic.common.core.web.controller.BaseController;
import com.openlogic.common.core.web.domain.entity.SysDept;
import com.openlogic.common.core.web.domain.entity.SysRole;
import com.openlogic.common.core.web.domain.entity.SysUser;
import com.openlogic.common.core.web.domain.model.LoginUser;
import com.openlogic.common.security.entity.SysTenantEntity;
import com.openlogic.system.api.RemoteSysUserService;
import com.openlogic.system.service.ISysDeptService;
import com.openlogic.system.service.ISysMenuService;
import com.openlogic.system.service.ISysRoleService;
import com.openlogic.system.service.ISysTenantService;
import com.openlogic.system.service.ISysUserService;

import cn.hutool.core.util.ObjectUtil;

/**
 * 远程调用系统服务实现 - 用户
 * @author xm
 */
@Remoting
@Mapping(ServiceConstant.PATH_TO_SYSTEM_USER)
public class RemoteSysUserServiceImpl extends BaseController implements RemoteSysUserService {

    @Inject
    ISysUserService sysUserService;
    
    @Inject
    ISysDeptService sysDeptService;

    @Inject
    ISysRoleService sysRoleService;

    @Inject
    ISysMenuService sysMenuService;
    
    @Inject
    ISysTenantService sysTenantService;

    /**
     * 根据用户名查询用户
     * @param userName
     */
    @Override
    public Result<LoginUser> getSysUserByUserName(String enterpriseName, String userName) {
    	SysTenantEntity enterprise = sysTenantService.selectEnterpriseByName(enterpriseName);
    	// 不存在直接返回空数据 | 与网络调用错误区分
    	if (ObjectUtil.isNull(enterprise)) 
    		// 企业账号不存在
    		Result.failure("企业账号不存在");

        // 存储企业账号id
        HeadersUtil.setHeader("tenantId", enterprise.getId());
        // 存储是否超管租户的标识
        HeadersUtil.setHeader("tenantName", enterpriseName);
        SysUser sysUser = sysUserService.selectSysUserByUserName(userName);
        if(ObjectUtil.isNull(sysUser)) {
            return Result.failure("用户名错误");
        }
        SecurityContextHolder.setEnterpriseId(enterprise.getId());
        SecurityContextHolder.setEnterpriseName(enterpriseName);
        SecurityContextHolder.setUserName(userName);
        // 角色集合
        Set<String> roles = sysRoleService.getRolePermission(sysUser.getUserId());
        // 权限集合
        Set<String> permissions = sysMenuService.getMenuPermission(sysUser);

        LoginUser loginUser = new LoginUser();
        loginUser.setEnterpriseId(enterprise.getId());
        loginUser.setEnterpriseName(enterpriseName);
        loginUser.setSysUser(sysUser);
        loginUser.setRoles(roles);
        loginUser.setPermissions(permissions);
        return Result.succeed(loginUser);
    }

    /**
     * 校验账号是否唯一
     * @param sysUser
     * @return
     */
    @Override
    public Result<Boolean> checkUserNameUnique(SysUser sysUser) {
        return Result.succeed(sysUserService.checkUserNameUnique(sysUser));
    }

    /**
     * 注册用户
     * @param sysUser
     * @return
     */
    @Override
    public Result<Integer> registerUser(SysUser sysUser) {
        return Result.succeed(sysUserService.registerUser(sysUser));
    }

	@Override
	public Result<List<SysUser>> selectUserList(SysUser sysUser) {
		return Result.succeed(sysUserService.selectUserList(sysUser));
	}

	@Override
	public Result<List<SysDept>> selectDeptList(SysDept sysDept) {
		return Result.succeed(sysDeptService.selectDeptList(sysDept));
	}

	@Override
	public Result<List<SysRole>> selectRoleList(SysRole sysRole) {
		return Result.succeed(sysRoleService.selectRoleList(sysRole));
	}

}
